{include file="my/header"}
<div class="container-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格数据查询块 -->
            <div class="table-query">
                <form class="layui-form layui-inline J-search-form" action="">
                    <div class="layui-inline">
                        <label class="layui-form-label">搜索ID：</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="id" autocomplete="off" placeholder="请输入id">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">课程:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="coursename" autocomplete="off" placeholder="请输入课程">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">考试任务:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="name" autocomplete="off" placeholder="请输入考试任务">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">审核状态:</label>
                        <div class="layui-inline">
                            <select name="check" class="layui-input J-query-params">
                                <option value="" selected>请选择审核状态</option>
                                <option value="0">未审核</option>
                                <option value="1">已通过</option>
                                <option value="2">未通过</option>
                            </select>
                            <!-- <input class="layui-input J-query-params" name="check" autocomplete="off" placeholder="请输入内容"> -->
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">发布时间:</label>
                        <div class="layui-inline">
                            <input type="text" class="layui-input J-query-params J-date" name="create_time" placeholder="yyyy-MM-dd">
                        </div>
                    </div>

                </form>
                <button class="layui-btn J-search" data-type="reload">搜索</button>
                <button class="layui-btn J-reload">重置</button>
            </div>
            <!-- 表格 -->
            <table class="layui-hide" id="testTable" lay-filter="test"></table>
            <!-- 表格工具 -->
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                     {if condition="session('userclass') eq 5 or session('userclass') eq 2"}
                     <!-- <button class="layui-btn layui-btn-sm" lay-event="add">添加</button> -->
                     <button class="layui-btn layui-btn-sm" lay-event="batchdel">批量删除</button>
                     {/if}
                    </div>

             </script>
            <script type="text/html" id="barDemo">
                 <a class="layui-btn layui-btn-xs" lay-event="edit">审核</a>
        {if condition="session('userclass') eq 5 or session('userclass') eq 2"}
                 <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        {/if}

             </script>
        </div>
        <div class="margin-top30 J-change-test is-hidden">
            <form class="layui-form layui-fluid J-change-form" lay-filter="formTest">
                <input type="hidden" class="form-control" id="id" name="id" value="" placeholder="请输入内容">
                <input type="hidden" class="form-control" id="schoolid" name="schoolid" value="985" placeholder="请输入内容">
                <input type="hidden" class="form-control" id="uid" name="uid" value="1" placeholder="请输入内容">

                <div class="layui-form-item">
                    <label class="layui-form-label">考试任务</label>
                    <div class="layui-input-block">
                        <select name="taskid" id="taskid">
                            . {volist name='taskdata2' id='tasklist'}
                            <option value="{$tasklist.id}">{$tasklist.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">监考记录</label>
                    <div class="layui-input-block">
                        <select name="resultid" id="resultid">
                            <option value="">请选择</option>
                            {volist name='dblistdata' id='data'}
                            <option value="{$data.id}">{$data.coursename}\{$data.teachername}\{$data.roomname}\{switch
                                $data.role}{case 1}副监考{/case}{case 2}巡考{/case}{default /}主监考{/switch}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">申请教师</label>
                    <div class="layui-input-block">
                        <select name="teacherid" id="teacherid">
                            <option value="">请选择</option>
                            {volist name='teacherinfodata' id='teacherdata'}
                            <option value="{$teacherdata.teacherid}">{$teacherdata.teachername}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">换新老师</label>
                    <div class="layui-input-block">
                        <select name="newteacherid" id="newteacherid">
                            <option value="">请选择</option>
                            {volist name='teacherinfodata' id='teacherdata'}
                            <option value="{$teacherdata.teacherid}">{$teacherdata.teachername}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $(function () {
        initClass('changeresultlist')
        initTable();
        layuiDate(".J-date")

        // 编辑
        $(".J-edit").click(function () {
            var that = $(this),
                data = {
                    id: that.attr('data-id')
                },
                type = that.attr('data-type');

            layerOpenInit({
                title: "审核",
                btnTitle: ['通过', '不通过']
            }, layerTemp, function (index) {
                type == 1 ? data.check1 = 1 : data.check2 = 1;
                data.type = type;
                ajaxRequest("post", "{:url('test/changecheck')}", data, function (data) {
                    data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                })
            }, function () {
                ajaxRequest("get", "{:url('test/changecheck')}", {
                    id: that.attr('data-id')
                }, function (data) {
                    $("#id").val(data.msg.id);
                    $("#schoolid").val(data.msg.schoolid);
                    $("#taskid").val(data.msg.taskid);
                    $("#resultid").val(data.msg.resultid);
                    $("#newteacherid").val(data.msg.newteacherid);
                    $("#teacherid").val(data.msg.teacherid);
                }, function () {
                    type == 1 ? data.check1 = 2 : data.check2 = 2;
                    data.type = type;
                    ajaxRequest("post", "{:url('test/changecheck')}", data, function (data) {
                        data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                    })
                })
            })
            return false;
        })

        // 删除
        $(".J-remove").click(function () {
            var that = $(this);
            layerConfirm("{:url('test/changeresultremove')}", {
                id: that.attr('data-id')
            })
        });

    })



    function initTable() {
        var table;
        var isHide = $("#isTeacher").prop('name') == 'isTeacher';

        // 请求路径
        var requireUrl = {
            add: "{:url('test/courseadd')}",
            check: "{:url('test/changecheck')}",
            remove: "{:url('test/changeresultremove')}",
            list: "{:url('test/getchangelist')}"
        }

        layui.use('table', function () {
            table = layui.table;
            table.render({
                elem: '#testTable'
                , url: requireUrl.list
                , toolbar: '#toolbarDemo'
                , title: '考试课程'
                , totalRow: false
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'id', title: '序号', }
                    , { field: 'name', title: '考试任务', width: 200 }
                    , { field: 'resultid', title: '监考记录', }
                    , { field: 'teachername', title: '申请老师', }
                    , { field: 'newteacher', title: '换新老师', }
                    , {
                        field: 'check1', title: '院系教务员审核', templet: d => {
                            var result;
                            d.check1 == 0 && (result = '<span class="">未审核</span>');
                            d.check1 == 1 && (result = '<span class="green">通过</span>');
                            d.check1 == 2 && (result = '<span class="red">不通过</span>');
                            return result;
                        },
                    }
                    , {
                        field: 'check2', title: '教务处审核', templet: d => {
                            var result;
                            d.check2 == 0 && (result = '<span class="">未审核</span>');
                            d.check2 == 1 && (result = '<span class="green">通过</span>');
                            d.check2 == 2 && (result = '<span class="red">不通过</span>');
                            return result;
                        },
                    }
                    , { field: 'create_time', title: "申请时间", }

                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 120, hide: isHide }
                ]]
                , id: 'testReload'
                , page: true
                , parseData: function (res) {
                    return {
                        "code": 0,
                        "msg": "",
                        "count": res.length,
                        "data": res
                    };
                }
            });

            //工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'add':
                        tableAdd({
                            addUrl: requireUrl.add,
                            layerTemp: $('.J-change-test'),
                            title: '发布考试课程'
                        });
                        break;
                    case 'batchdel':
                        tableBatchDel(checkStatus, requireUrl.remove);
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                console.log(data.id)
                if (obj.event === 'del') {
                    layerConfirm(requireUrl.remove, {
                        id: data.id
                    })
                } else if (obj.event === 'edit') {
                    var data = {
                        id: data.id
                    }

                    layerOpenInit({
                        title: "审核",
                        btnTitle: ['通过', '不通过']
                    }, $('.J-change-test'), function (index) {
                        data.check = 1;
                        ajaxRequest("post", requireUrl.check, data, function (data) {
                            data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                        })
                    }, function () {
                        ajaxRequest("get", requireUrl.check, {
                            id: data.id
                        }, function (res) {
                            form.val("formTest", res.data);
                        })
                    }, function () {
                        data.check = 2;
                        ajaxRequest("post", requireUrl.check, data, function (data) {
                            data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                        })
                    })
                }
            });
        })

        // 搜索重置操作
        var active = {
            reload: function () {
                var queryParams = serializeObject($(".J-search-form"));
                console.log(queryParams);
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: queryParams
                }, 'data');
            }
        };
        $('.J-search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.J-reload').on('click', function () {
            $('.J-query-params').val('');
            form.render('select');
            active.reload ? active.reload.call(this) : '';
        });

    }

</script>

{include file="my/footer"}